package com.wit.dao.impl;

import hibernate.HibernateSessionFactory;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.wit.dao.MdescriptionDao;
import com.wit.po.Mark;
import com.wit.po.Mdescription;

public class MdescriptionDaoImpl implements MdescriptionDao {

	
	public boolean addMdescription(Mdescription mdescription) {
		Session s = null;
		Transaction tx = null;
		boolean isFinished = false;
		try{
			s = HibernateSessionFactory.getSession();
			tx =  s.beginTransaction();
			s.save(mdescription);
			tx.commit();
			s.close();
			isFinished = true;
		}catch(HibernateException e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return isFinished;
	}

	
	public boolean deleteMdescription(Mdescription mdescription) {
		Session s = null;
		Transaction tx = null;
		boolean isFinished = false;
		try{
			s = HibernateSessionFactory.getSession();
			tx =  s.beginTransaction();
			s.delete(mdescription);
			tx.commit();
			s.close();
			isFinished = true;
		}catch(HibernateException e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return isFinished;
	}

	
	public List<Mdescription> findMdescriptionByMark(Mark mark) {
		List<Mdescription> compList = new ArrayList<Mdescription>();
		Session s = null;
		try{
			s = HibernateSessionFactory.getSession();
			compList = (List<Mdescription>)s.createQuery("FROM Mdescription where mark=?").setParameter(0, mark).list();
		}catch(HibernateException e){
			e.printStackTrace();
		}
		return compList;
	}

	
	public Mdescription findByIdentifier(Integer mdid) {
		Session s = null;
		Mdescription mdescription = null;
		try{
			s = HibernateSessionFactory.getSession();
			List<Mdescription> compList = (List<Mdescription>)s.createQuery("FROM Mdescription WHERE mdid = ?").setInteger(0, mdid).list();
			if(compList.size() == 1)
				mdescription = compList.get(0);
			s.close();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return mdescription;
	}

	
	public boolean modifyMdescription(Mdescription mdescription) {
		Session s = null;
		Transaction tx = null;
		boolean isFinished = false;
		try{
			s = HibernateSessionFactory.getSession();
			tx =  s.beginTransaction();
			s.update(mdescription);
			tx.commit();
			s.close();
			isFinished = true;
		}catch(HibernateException e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return isFinished;
	}

}
